package scau.erps.view.system;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;

import scau.erps.app.Erps;
import scau.erps.entity.Sand;
import scau.erps.service.SandService;
import scau.erps.service.StepLimitService;
import scau.erps.service.impl.SandServiceImpl;
import scau.erps.service.impl.StepLimitServiceImpl;
import scau.erps.util.DBcontrol;
import scau.erps.view.business.BusinessFrame;
import scau.erps.view.system.teacher.SystemOrderManagementDialog;

/**
 * 系统管理窗口_教师
 *  
 * @author DCJ
 *
 */
public class SystemManagementFrame extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JPanel schedulePanel,scorePanel;
	private JLabel[] steps = new JLabel[8];
	private JToolBar toolBar;
	private JButton biddingBtn,refreshBtn,dbBakBtn,dbRepairBtn,orderBtn,businessBtn;
	private JPanel contentPanel = new JPanel();	
	private JTabbedPane tabbedPane;
	public SystemManagementFrame(){
		this.setTitle("ERP沙盘教学辅助系统 "+Erps.version);			
		this.setSize(1024, 768);
		this.setResizable(true);
		this.setLocationRelativeTo(null);
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BorderLayout());
		leftPanel.add(getSchedulePanel(),BorderLayout.NORTH);
		leftPanel.add(getInteractPanel(),BorderLayout.CENTER);			
		contentPanel.setBorder(BorderFactory.createEtchedBorder());
		contentPanel.setLayout(new BorderLayout());		
		contentPanel.add(getToolBar(), BorderLayout.NORTH);		
		contentPanel.add(leftPanel,BorderLayout.WEST);
		contentPanel.add(getScorePanel(),BorderLayout.CENTER);
		contentPanel.add(getCopyrightPane(),BorderLayout.SOUTH);
		this.setContentPane(contentPanel);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		TimerTask timerTask=new TimerTask() {
			@Override
			public void run() {
				refreshSchedule();                
			}
		};
		Timer timer=new Timer();
		timer.scheduleAtFixedRate(timerTask, 0, 10000);  //每3s进行一次进度刷新
	}	
	private JToolBar getToolBar(){
		toolBar = new JToolBar();
		biddingBtn = new JButton("开始竞价");
		refreshBtn = new JButton("刷新成绩");
		orderBtn = new JButton("订单管理");
		businessBtn = new JButton("业务管理");
		dbBakBtn = new JButton("数据备份");
		dbRepairBtn =  new JButton("数据初始化");		
		biddingBtn.addActionListener(this);		
		refreshBtn.addActionListener(this);
		orderBtn.addActionListener(this);
		dbBakBtn.addActionListener(this);
		dbRepairBtn.addActionListener(this);
		businessBtn.addActionListener(this);
		toolBar.add(refreshBtn);
		toolBar.add(biddingBtn);		
		toolBar.addSeparator();
		toolBar.add(orderBtn);
		toolBar.add(businessBtn);
		toolBar.addSeparator();
		//toolBar.add(dbBakBtn);
		toolBar.add(dbRepairBtn);
		return toolBar;			
	}
	
	/**
	 * 状态栏
	 * @return
	 */
	private JPanel getCopyrightPane() {		
		JPanel	copyrightPanel = new JPanel();
		JLabel label = new JLabel("版权所有：华南农业大学信息学院 ");
		copyrightPanel.add(label);
		return copyrightPanel;
	}

	
	/**
	 * 进度面板
	 * @return
	 */
	private JPanel getSchedulePanel(){
		if(schedulePanel ==null){
			schedulePanel = new JPanel();			
		}
		schedulePanel.setBorder(BorderFactory.createTitledBorder("查看进程进度"));
		schedulePanel.setPreferredSize(new Dimension(150,350));
		schedulePanel.setLayout(new GridLayout(8,2,2,2));
		JLabel[] teams = new JLabel[8];
		for (int i = 0; i < teams.length; i++) {
			int team_Id = i+1;
			teams[i] = new JLabel("第"+team_Id+"组:");
		}
		for (int i = 0; i < steps.length; i++) {			
			steps[i] = new JLabel();
		}
		for (int i = 0; i < steps.length; i++) {
			schedulePanel.add(teams[i]);
			schedulePanel.add(steps[i]);
		}
		this.refreshSchedule();
		return schedulePanel;
	}
	/**
	 * 刷新查看各小组进度
	 */
	private void refreshSchedule(){
		SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);	
		Sand sand = null;
		for (int j = 0; j < steps.length; j++) {
			int team_Id = j+1;
			sand = sandService.getSandByTeam(team_Id);
			steps[j].setText(sand.getTime()+"-"+sand.getStep());			
		}		
	}
	/**
	 * 互动信息面板
	 * @return
	 */
	public JPanel getInteractPanel(){
		JPanel interactPanel = new JPanel();
		interactPanel.setBorder(BorderFactory.createTitledBorder("互动区"));
		interactPanel.setPreferredSize(new Dimension(150,350));
		return interactPanel;		
	}
	/**
	 * 成绩类面板
	 * @return
	 */
	private JPanel getScorePanel(){
		if(scorePanel == null){
			scorePanel = new JPanel();
		}
		scorePanel.setBorder(BorderFactory.createTitledBorder("成绩排行"));
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		//SandResultsPanel sandResultsPanel2 = new SandResultsPanel();
		SandResultsPanel sandResultsPanel = new SandResultsPanel();
		tabbedPane.add(sandResultsPanel, "沙盘成绩");
		//tabbedPane.add(sandResultsPanel2, "企业发展分析表");
		scorePanel.add(tabbedPane, BorderLayout.NORTH);
		scorePanel.setPreferredSize(new Dimension(700,500));
		return scorePanel;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==refreshBtn){
			this.refreshSchedule();
			tabbedPane.removeAll();
            tabbedPane.add(new SandResultsPanel(), "沙盘成绩");
		}
		else if(e.getSource()==orderBtn){
			new SystemOrderManagementDialog();
		}
		else if(e.getSource()==businessBtn){
			new BusinessFrame();
		}
		else if(e.getSource()==biddingBtn){
			StepLimitService stepLimitService=Erps.getIocInstance().get(StepLimitServiceImpl.class);
			stepLimitService.setStepLimit(3);
			JOptionPane.showMessageDialog(null, "本年度订单竞价已开始,各小组请进入竞价界面","提示",JOptionPane.INFORMATION_MESSAGE);
		}
		else if(e.getSource()==dbRepairBtn){
			int flag=JOptionPane.showConfirmDialog(null, "该操作将删除当前进度的数据，恢复到Y0初的初始状态，确定初始化数据？","警告",JOptionPane.YES_NO_OPTION);
			if (flag==JOptionPane.YES_OPTION){
				DBcontrol.reset();
			}
		}
		else if(e.getSource()==dbBakBtn){
			//DBcontrol.export();
		}
		
	}
}
